package com.jinyang.jetpackdemo.activity

import android.Manifest
import android.os.Bundle
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.activity.ui.MyDialogFragment
import com.jinyang.jetpackdemo.util.permissionsRequest
import com.jinyang.jetpackdemo.util.permissionsRequestEach
import kotlinx.android.synthetic.main.activity_progress.*


class ProgressActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_progress)
        progress_bar1.maxCount = 30F
        progress_bar1.currentCount = 10F
        progress_bar1.setText("试用期赠送额度     10天")


        progress_bar2.maxCount = 30F
        progress_bar2.currentCount = 20F
        progress_bar2.setText("试用期赠送额度     10天")


        progress_bar3.maxCount = 30F
        progress_bar3.currentCount = 30F
        progress_bar3.setText("试用期赠送额度     10天")


        progress_bar1.post {
            val lp: RelativeLayout.LayoutParams = ll_1.layoutParams as RelativeLayout.LayoutParams
            lp.rightMargin =
                (progress_bar1.height * 2 + (progress_bar1.width * (progress_bar1.maxCount - progress_bar1.currentCount) / progress_bar1.maxCount).toInt()).toInt()
            ll_1.layoutParams = lp
            val lp2: RelativeLayout.LayoutParams = ll_2.layoutParams as RelativeLayout.LayoutParams
            lp2.rightMargin =
                (progress_bar1.height * 1.2 + (progress_bar2.width * (progress_bar2.maxCount - progress_bar2.currentCount) / progress_bar2.maxCount).toInt()).toInt()
            ll_2.layoutParams = lp2
            val lp3: RelativeLayout.LayoutParams = ll_3.layoutParams as RelativeLayout.LayoutParams
            lp3.rightMargin =
                (progress_bar1.height * 1.2 + (progress_bar3.width * (progress_bar3.maxCount - progress_bar3.currentCount) / progress_bar3.maxCount).toInt()).toInt()
            ll_3.layoutParams = lp3
        }

        val dialog1 = MyDialogFragment()
        dialog1.setTitle("什么是无流观看？")
        dialog1.setContent("\t对不起！因家长购买率不够，无法完成支撑贵班教师云直播流转化，平台成本压力剧增！教育信息化是一项普惠性事业，坚持公益为主、略有盈余原则下，平台推出最低普惠政策，当家长参与人数达20人，平台承诺班级教师可免费观看本学期。")
        dialog1.setContent2("\t为提升家长参与积极性，实用性，请任课教师经常使用平台以下应用：")
        dialog1.setContent3("1.班主任通过任课表选择多开放直播课给家长；\n2.大力推动本班电子请假条，实现班级请假云模式；\n3.经常发布班级云通知，要求家长阅读并签收；\n4.通过平台学生管理栏目，推动学生评价工作开展，建议各任课教师每月每生一条70字评价；")
        dialog1.setHeight(2200)
        ll_2.setOnClickListener {
            dialog1.showNow(supportFragmentManager, "MyDialogFragment1")
        }

        val dialog2 = MyDialogFragment()
        dialog2.setTitle("关于无鲜花奖？")
        dialog2.setContent("\t对不起！您班级家长当前支撑购买人数未达到平台规定的最低30人任务，无鲜花赠送。请多开展平台应用，提升家长使用习惯和满意值，当达到30人，平台鲜花赠送机制激活，按每人20朵计算，30人可赠送600朵。")
        dialog2.setContent3("\t赠送鲜花可在平台鲜花明细栏目申请平台家庭教育指导师工作，通过开展平台委托的家庭教育指导师岗位任务，实现鲜花一比一转化岗位收入。")
        dialog2.setHeight(1500)

        ll_3.setOnClickListener {
            dialog2.showNow(supportFragmentManager, "MyDialogFragment1")
        }
    }

    override fun onStart() {
        super.onStart()
        //所有权限统一返回结果
        permissionsRequest(
            arrayOf(
                Manifest.permission.CAMERA,
                Manifest.permission.SEND_SMS,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
            )
        ){
            if (it) {
                todoSomething()
            }else {
                Toast.makeText(this, "我们需要相应的权限，请允许权限申请", Toast.LENGTH_LONG).show()
            }
        }
        //将权限申请结果逐一返回
        permissionsRequestEach(
            arrayOf(
                Manifest.permission.CAMERA,
                Manifest.permission.SEND_SMS,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
            )
        ){
            when {
                it.granted -> {
                    // `permission.name` is granted !
                    log("Activity.允许了权限申请:" + it.name);
                }
                it.shouldShowRequestPermissionRationale -> {
                    // Denied permission without ask never again
                    log("Activity.取消了权限申请:" + it.name);
                }
                else -> {
                    // Denied permission with ask never again
                    // Need to go to the settings
                    log("Activity.拒绝并且点了“不再询问”的权限:" + it.name);
                }
            }
        }
    }

    private fun todoSomething() {

    }
}